package com.ar.datahandler;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ar.bean.Exam;
import com.ar.bean.Quiz;
import com.ar.network.NetworkUtility;

public class StringJSONHandler {
	
	public boolean checkError(String jsonResponse){
		JSONObject jsonTemp;
		int errorCode = 1;
		try {
			jsonTemp = new JSONObject(jsonResponse);
			errorCode = jsonTemp.getInt(NetworkUtility.ERROR_CODE);	
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if(errorCode != 0) return true;
		else return false;
	}
	
	public boolean checkDataAvailable(String jsonResponse){
		//try {
			//JSONObject jsonTemp = new JSONObject(jsonResponse);
			//JSONArray data = jsonTemp.getJSONArray(NetworkUtility.DATA);
			//if (data.length() < 1) return false;
			if (fillDataFromResponse(jsonResponse).getContent().size() < 1) return false;
		//} catch (JSONException e) {
		//	e.printStackTrace();
		//}
		return true;
	}
	
	public String getErrorMessage(String jsonResponse){
		JSONObject jsonObject;
		String msg = "";
		try {
			jsonObject = new JSONObject(jsonResponse);
			msg = jsonObject.getString(NetworkUtility.ERROR_MESSAGE);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return msg;
	}
	
	/*public String getErrorCode(String jsonResponse){
		JSONObject jsonObject;
		String errCode = "";
		try {
			jsonObject = new JSONObject(jsonResponse);
			errCode = jsonObject.getString(NetworkUtility.ERROR_CODE);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return errCode;
	}*/
	
	
	public Exam fillDataFromResponse(String json){
		ArrayList<Quiz> alResult = new ArrayList<Quiz>();
		Exam aExam = new Exam();
		try {
			JSONObject jsonTemp = new JSONObject(json);
			aExam.setTime(50);
			String data = jsonTemp.getString(NetworkUtility.DATA);// sửa lại sau cho đúng với server
			JSONArray jsonData = new JSONArray(data); //sửa lại sau cho đúng với server
			for (int i = 0; i < jsonData.length(); i++) {
				JSONObject jsonObject = jsonData.getJSONObject(i);
				int id = jsonObject.getInt(NetworkUtility.ID);
				String category = jsonObject.getString(NetworkUtility.CATEGORY);
				String question = jsonObject.getString(NetworkUtility.QUESTION);
				String type = jsonObject.getString(NetworkUtility.TYPE);
				String description = jsonObject.getString(NetworkUtility.IMAGE_DESCRIPTION);
				//String[] answers = (String[]) jsonObject.get(ANSWERS);
				String abc = jsonObject.getString(NetworkUtility.ANSWERS); //sửa lại sau cho đúng với server
				JSONArray ansJsonArray;
				try{
				ansJsonArray = new JSONArray(abc);
				} catch (JSONException e) {
					continue; 
				}
				String[] answers = new String[ansJsonArray.length()]; 
				for (int j = 0 ; j<ansJsonArray.length(); j++) answers[j] = ansJsonArray.getString(j); 
				int num_answer = answers.length;
				String imageUrl = jsonObject.getString(NetworkUtility.IMAGE_URL);
				Quiz aQuiz = new Quiz(id,category, num_answer, question, type, answers, imageUrl,description, 0.25f);
				alResult.add(aQuiz);
			}
			aExam.setContent(alResult);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return aExam;
		
	}
	/*
	public ResultHolder doRrepareForUserResult(String jsonResponse, ArrayList<Integer> ansIDs, int[] userAnswers){
		ArrayList<Integer> alCorrectOrder = new ArrayList<Integer>();
		ArrayList<String> alResult = new ArrayList<String>();
		int num_correct = 0;
		float mark = 0;
		int[] trueAnswers = null;
		boolean ansFlags[] = null;
		try {
			JSONObject jsonResult = new JSONObject(jsonResponse);
			mark = (float) jsonResult.getDouble(NetworkUtility.MARK);
			JSONArray jsonArray = jsonResult.getJSONArray(NetworkUtility.RESULTS);
			trueAnswers = new int[jsonArray.length()];
			ansFlags = new boolean[jsonArray.length()];
			alCorrectOrder = getCorrectOrderListForShow(jsonArray, ansIDs);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(alCorrectOrder.get(i));
				String check = jsonObject.getString(NetworkUtility.CHECK);
				if (check.equals("1")) {
					num_correct++;
					ansFlags[i] = true;
				} else
					ansFlags[i] = false;
				
				String abc = jsonObject.getString(NetworkUtility.TRUE_ANSWERS);
				JSONArray arrTrue =  new JSONArray(abc);
				for (int j = 0; j < arrTrue.length(); j++) {
					trueAnswers[i] = (Integer) arrTrue.get(j);
					String trueAnswer = toWord((Integer) arrTrue.get(j));
					String userAnswer = toWord(userAnswers[i]);
					if (trueAnswer.equals(userAnswer))
						alResult.add("<b><u><font color='#04bd84'>" + (i + 1)
								+ "</font></u>" + ". " + trueAnswer+"</b>");
					else {
						alResult.add("<b><u><font color='#04bd84'>" + (i + 1)
								+ "</font></u>" + ". " + userAnswer + "-> "
								+ "<font color='#dc5d54'>" + trueAnswer
								+ "</font></b>");
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
//		return new ResultHolder(mark, num_correct,trueAnswers, ansFlags, alResult);
		return null;
		
	}
	*/
	
	private ArrayList<Integer> getCorrectOrderListForShow(JSONArray jsonArray,ArrayList<Integer> ansIDs) throws JSONException{
		ArrayList<Integer> order = new ArrayList<Integer>();
		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			int ID = jsonObject.getInt(NetworkUtility.ID);
			for (int x = 0; x < ansIDs.size(); x++) {
				int temp = ansIDs.get(x);
				if (ID == temp)
					order.add(ansIDs.indexOf(temp));
			}
		}
		return order;	
	}
	
	public static String toWord(int answer_id) {
		switch (answer_id) {
		case 0:
			return "A";
		case 1:
			return "B";
		case 2:
			return "C";
		case 3:
			return "D";
		case 4:
			return "E";
		case 5:
			return "F";
		case 6:
			return "G";
		case 7:
			return "H";
		case 8:
			return "I";
		case 9:
			return "J";
		case 10:
			return "K";
		default:
			return "";
		}
	}
	
}
